<?php
include 'checkLogin.php';

@$goodsId = $_REQUEST['goodsId'];
@$noPayOrderId = $_REQUEST['noPayOrderId'];

include 'db/li.php';

@$price = dbSelect('select sellPrice,stock,sellStatus from goods where id=:id',array('id'=>$goodsId));
@$count = dbSelect('select `count` from goods_order where goods_order.orderId=:oid and goods_order.goodsId=:gid',
						array('oid'=>$noPayOrderId,'gid'=>$goodsId));
//@$stock = dbSelect('select stock from goods where id=:id',array('id'=>$goodsId));

$newStock = $count['count'] + $price['stock'];
if($price['sellStatus'] == "缺货"){		//还原商品库存及销售状态
	@$countFlog = dbUpdate("update goods set stock=:stock,sellStatus='上架' where id=:id",array('stock'=>$newStock,'id'=>$goodsId));
}else if($price['sellStatus'] == "上架"){
	@$countFlog = dbUpdate("update goods set stock=:stock where id=:id",array('stock'=>$newStock,'id'=>$goodsId));
}



$allPrice = $price['sellPrice']*$count['count'];		//这次商品总价价格

@$goodsPrice = dbSelect('select goodsPrice from `order` where id=:id',array('id'=>$noPayOrderId));		//订单商品总价

$newPrice = $goodsPrice['goodsPrice'] - $allPrice;		//选商品后总价


@$priceFlog = dbUpdate("update `order` set goodsPrice=:goodsPrice where id=:id",	//修改count
		array('goodsPrice'=>$newPrice,'id'=>$noPayOrderId));
if(!$priceFlog){
	$mes = '获取价格失败';
	include 'index.php';
	die;
}

@$flog = dbDelete('delete from goods_order where goods_order.goodsId=:gid and goods_order.orderId=:oid',		//取消商品
		array('gid'=>$goodsId,'oid'=>$noPayOrderId));
if(!$priceFlog){
	$mes = '取消商品失败';
	include 'index.php';
	die;
}
header('Refresh:0;url=index.php?noPayOrderId='.$noPayOrderId);